package com.example.homemodule.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.examination.mlib.constants.AllStringConstants;
import com.examination.mlib.constants.ClickApi;
import com.examination.mlib.constants.KitArouterConstants;
import com.examination.mlib.bean.HomeDrugEntity;
import com.example.homemodule.R;
import com.example.homemodule.utils.HomeUtils;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.GlideUtil;
import com.yilijk.base.utils.ListUtil;

import java.text.DecimalFormat;
import java.util.List;

public class HomeDrugListAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater inflater;
    private List<HomeDrugEntity.DrugItemData> drugs;

    public HomeDrugListAdapter(Context context, List<HomeDrugEntity.DrugItemData> drugs) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.drugs = drugs;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new DrugHolder(inflater.inflate(R.layout.item_home_drug, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof DrugHolder) {
            ALog.e("oakkk", "on bind view ==> : " + drugs.get(position).getName() + " " + position);
            ((DrugHolder) holder).setData(position);
        }
    }

    @Override
    public int getItemCount() {
        return ListUtil.getSize(drugs);
    }

    class DrugHolder extends RecyclerView.ViewHolder {

        ImageView drugIv;
        LinearLayout tagLl;
        TextView drugNameTv;
        TextView drugNameTv2;
        TextView rxTv;
        TextView discountTv;
        TextView priceTv;
        TextView oldPriceTv;
        FrameLayout nameFl;
        View fugouIIv;
        TextView tv_reduceRule, tv_supportGive;

        public DrugHolder(@NonNull View itemView) {
            super(itemView);
            drugIv = itemView.findViewById(R.id.iv_drug);
            tagLl = itemView.findViewById(R.id.ll_tag);
            drugNameTv = itemView.findViewById(R.id.tv_drug_name);
            drugNameTv2 = itemView.findViewById(R.id.tv_drug_name_2);
            rxTv = itemView.findViewById(R.id.tv_rx);
            discountTv = itemView.findViewById(R.id.tv_discount);
            priceTv = itemView.findViewById(R.id.tv_price);
            oldPriceTv = itemView.findViewById(R.id.tv_old_price);
            nameFl = itemView.findViewById(R.id.fl_name_2);
            fugouIIv = itemView.findViewById(R.id.iv_fugou);
            tv_supportGive = itemView.findViewById(R.id.tv_supportGive);
            tv_reduceRule = itemView.findViewById(R.id.tv_reduceRule);
        }

        public void setData(int position) {
//            GlideUtil.load(context, "https://imgvbp.yilijk.com/bd3/bd3afbef594a4f6a907ae35c93641bc9.jpg", drugIv);
            HomeDrugEntity.DrugItemData drug = drugs.get(position);
//            GlideUtil.load(context, drug.getImageUrl(), drugIv, R.drawable.icon_default_square);
            GlideUtil.load(context, drug.getImageUrl() + AllStringConstants.imgZipStr, drugIv, R.drawable.icon_default_square);
//            SpannableStringBuilder ssb = new SpannableStringBuilder(drug.getName());
//            ssb.setSpan(new DrawableMarginSpan(, 6), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            drugNameTv.setText(ssb);
            if (drug.isRegular()) {
                drugNameTv.setVisibility(View.GONE);
                nameFl.setVisibility(View.VISIBLE);
                drugNameTv2.setText(String.format(context.getResources().getString(R.string.space_start_3), drug.getName()));
            } else {
                nameFl.setVisibility(View.GONE);
                drugNameTv.setVisibility(View.VISIBLE);
                drugNameTv.setText(drug.getName());
            }
//            if (drug.isRx()) {
//                rxTv.setVisibility(View.VISIBLE);
//            } else {
//                rxTv.setVisibility(View.GONE);
//            }
            if (drug.isSupportReduce() && drug.getReduceRule() != null) {
                tv_reduceRule.setVisibility(View.VISIBLE);
                tv_reduceRule.setText("满" + drug.getReduceRule().getFullReduceRuleMoney() + "减" + drug.getReduceRule().getReduceMoney());
            } else {
                tv_reduceRule.setVisibility(View.GONE);
            }
            if (drug.isSupportGive()) {
                tv_supportGive.setVisibility(View.VISIBLE);
            } else {
                tv_supportGive.setVisibility(View.GONE);
            }
            if (drug.getIsGiveCoupon() == 1) {
                fugouIIv.setVisibility(View.VISIBLE);
            } else {
                fugouIIv.setVisibility(View.GONE);

            }

//            if (!TextUtils.isEmpty(drug.getDiscountStr()) && !TextUtils.isEmpty(drug.getDifferenceStr())) {
//                discountTv.setText(drug.getDiscountStr() + "折 省" + drug.getDifferenceStr() + "元");
//                discountTv.setVisibility(View.VISIBLE);
//            } else {
//                discountTv.setVisibility(View.GONE);
//            }

            String price = "¥" + getPrice(drug.getPrice());
            SpannableString priceSpan = new SpannableString(price);
            priceSpan.setSpan(new RelativeSizeSpan(1.5f), 1, price.indexOf("."), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            priceTv.setText(priceSpan);

            oldPriceTv.setPaintFlags(oldPriceTv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            if (!TextUtils.isEmpty(drug.getOriginalPrice()) && Double.parseDouble(drug.getOriginalPrice()) > 0) {
                oldPriceTv.setText("¥" + getPrice(drug.getOriginalPrice()));
                oldPriceTv.setVisibility(View.VISIBLE);
            } else {
                oldPriceTv.setText("");
                oldPriceTv.setVisibility(View.GONE);
            }

            ClickUtils.setFastOnClickListener(itemView, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ClickUtils.sendEvent(ClickApi.Home.click_drugslist,context,"首页药品列表点击");
                    Bundle bundle = new Bundle();
//                    if (drugEntity.getItemType() == ITEM_TYPE_O2O_CART_VOS) {
//                        O2oCartVOSEntity vosEntity = (O2oCartVOSEntity) drugEntity;
//                        bundle.putString("storeId", "" + vosEntity.getStoreId());
//                        bundle.putString("openTime", "" + vosEntity.getOpenTime());
//                        bundle.putString("businessScope", "" + vosEntity.getBusinessScope());
//                        bundle.putInt("scene", 2);
//                    } else {
//                        bundle.putInt("scene", 1);
//                    }
//                    if (whCityBean != null) {
//                        bundle.putString("addressId", whCityBean.getAddressId() + "");
//                        bundle.putDouble("latitude", whCityBean.getLat());
//                        bundle.putDouble("longitude", whCityBean.getLng());
//                    }

                    if ("drug".equals(drug.getCommodityType())) {
//                        bundle.putString("id", "" + drug.getDrugId());
                        bundle.putString("id", "" + drug.getCommodityId());
                    } else {
//                        bundle.putString("packetId", "" + drug.getPacketId());
                        bundle.putString("packetId", "" + drug.getCommodityId());

                    }
//                    bundle.putString("doctorId", drugEntity.getCommodityList().get(messageEvent.getChildPosition()).getDoctorId() + "");
                    ALog.e("oakkk", "bundle: " + bundle.toString());
                    HomeUtils.push(context,
                            KitArouterConstants.CART_DOCTORBUNDLE,
                            KitArouterConstants.Cart.DrugDetail,
                            bundle);
                }
            });

        }
    }

    private String getPrice(String price) {
        float f = Float.parseFloat(price);
        DecimalFormat decimalFormat = new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
        String p = decimalFormat.format(f);//format 返回的是字符串
        if (p.substring(0, 1).equals(".")) {
            p = "0" + p;
        }
        return p;
    }
}
